<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\model\activity\Cate;
use app\common\model\activity\Item;
use app\common\validate\AcitivityItem;
use think\Db;

/**
 * 实践活动控制器
 */
class Activity extends Api
{
    protected $noNeedLogin = ['detail','getList'];
    protected $noNeedRight = '*';

    /*
     * 活动分类
     * */
    public function getCate() {
        $cate = new Cate();
        $list = $cate->order('id asc')->select();

        $this->success('分类列表', $list);
    }

    /*
     * 根据分类获取活动
     * */
    public function getList() {

        $item = new Item();
        $org_ids = Db::name('org_cate')->where('parent_id', ORG_CATE_ID)->column('id');
        if($org_ids){
            $org_ids = array_merge([ORG_CATE_ID], $org_ids);
            $org_ids = Db::name('org_cate')->where('parent_id', 'in',$org_ids)->column('id');
        }
        $org_ids = array_merge([ORG_CATE_ID], $org_ids);
        $org_cate= Db::name('org_cate')->where('id', 'in', $org_ids)->field('id,name')->select();
        if($org_cate){
            foreach($org_cate as &$val){
                $where['org_cate_id'] = $val['id'];
                $val['item'] = $item->where($where)->order('createtime desc')->select();
            }
        }
        $this->success('活动列id表', $org_cate);
    }

    /*
     * 获取文章详情
     * */
    public function getDetail() {
        $id = input('id/d', 0);
        $info = Item::get($id);
        if (!$info) {
            $this->error('获取错误');
        }
        $this->success('活动详情', $info);
    }


    public function detail()
    {
        $id = $this->request->param('id/d', 0);
        $info = Item::get($id);

        return view('v1/web/activity',['data'=>$info]);
    }

    public function saveItem()
    {
        //数据验证
        (new AcitivityItem())->goCheck('saveitem');
        $data = $this->request->param();
        $data['user_id'] =UID;
        $data['org_cate_id'] = ORG_CATE_ID;
        $reply = new Item($this->request->param());
        $res = $reply->allowField(true)->isUpdate(false)->save();
        if (!$res) {
            $this->error('添加失败');
        }
        $this->success('添加成功');
    }

}
